package com.mj.guoqi.adapter;

import java.util.List;

import com.mj.guoqi.R;
import com.mj.guoqi.activity.GuoqiDetailActivity;
import com.mj.guoqi.bean.GuoqiDto;
import com.mj.guoqi.util.AppUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 国旗列表
 * @author zhaominglei
 * @date 2015-8-15
 * 
 */
public class GuoqiAdapter extends BaseAdapter {
	private Context context;
	private List<GuoqiDto> guoqiDtos;
	@SuppressWarnings("unused")
	private ProgressBar progressBar;

	public GuoqiAdapter(Context context, List<GuoqiDto> guoqiDtos) {
		super();
		this.context = context;
		this.guoqiDtos = guoqiDtos;
	}

	public List<GuoqiDto> getGuoqiDtos() {
		return guoqiDtos;
	}

	public void setGuoqiDtos(List<GuoqiDto> guoqiDtos) {
		this.guoqiDtos = guoqiDtos;
	}
	
	@Override
	public int getCount() {
		return guoqiDtos.size();
	}

	@Override
	public Object getItem(int position) {
		return guoqiDtos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (RelativeLayout) convertView;
		} else {
			relativeLayout = (RelativeLayout) View.inflate(context,
					R.layout.guoqi_item, null);
		}
		progressBar = (ProgressBar) relativeLayout.findViewById(R.id.guoqi_item_progress);
		ImageView imageView = (ImageView) relativeLayout.findViewById(R.id.guoqi_item_image);
		TextView nameTxt = ((TextView) relativeLayout.findViewById(R.id.guoqi_item_template_name));
		
		final GuoqiDto guoqiDto = guoqiDtos.get(position);
		nameTxt.setText(guoqiDto.getName());
		String imageName = guoqiDto.getLogo().replaceAll("img/", "").replaceAll(".gif", "");
		imageView.setImageResource(AppUtils.getResId(context, imageName, "drawable"));
		relativeLayout.setTag(guoqiDto.getGuoqiId());
		relativeLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Long descId = (Long)v.getTag();
				if (descId != null && descId.equals(guoqiDto.getGuoqiId())) {
					Intent intent = new Intent(v.getContext(), GuoqiDetailActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("guoqiDto", guoqiDto);
					intent.putExtras(bundle);
					v.getContext().startActivity(intent);
				}
			}
		});
		return relativeLayout;
	}
}
